package net.bblot.lottery.activity;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;

import net.bblot.lottery.PackegeConfig;
import net.bblot.lottery.util.AppConfig;
import net.bblot.lottery.util.FileUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Stack;

/**
 * 应用程序Activity管理类：用于Activity管理和应用程序退出
 * 
 * @author xiehuichao
 * @version 1.0
 * @created 2012-3-21
 */
public class AppManager {

	private static Stack<Activity> activityStack;
	private static AppManager instance;

	private AppManager() {
	}

	/**
	 * 单一实例
	 */
	public static AppManager getAppManager() {
		if (instance == null) {
			instance = new AppManager();
		}
		if (activityStack == null) {
			activityStack = new Stack<Activity>();
		}
		return instance;
	}

	/**
	 * 添加Activity到堆栈
	 */
	public void addActivity(Activity activity) {
		if (activityStack == null) {
			activityStack = new Stack<Activity>();
		}
		activityStack.add(activity);
	}

	/**
	 * 获取当前Activity（堆栈中最后一个压入的）
	 */
	public Activity currentActivity() {
		Activity activity = null;
		if (!activityStack.isEmpty()) {
			activity = activityStack.lastElement();
		}
		return activity;
	}

	/**
	 * 结束当前Activity（堆栈中最后一个压入的）
	 */
	public void finishActivity() {
		Activity activity = activityStack.lastElement();
		finishActivity(activity);
	}

	/**
	 * 结束指定的Activity
	 */
	public void finishActivity(Activity activity) {
		if (activity != null) {
			activityStack.remove(activity);
			activity.finish();
			activity = null;
		}
	}

	/**
	 * 结束指定类名的Activity
	 */
	public void finishActivity(Class<?> cls) {
		ArrayList<Activity> av_array = new ArrayList<Activity>();
		for (Activity activity : activityStack) {
			if (activity.getClass().equals(cls)) {
				av_array.add(activity);
			}
		}
		for (Activity activity : av_array) {
			activity.finish();
		}
	}

	/**
	 * 指定类名的Activity是否活跃
	 */
	public boolean isActivityAlive(Class<?> cls) {
		boolean flag = false;
		if (activityStack == null) {
			return false;
		}
		for (Activity activity : activityStack) {
			if (activity.getClass().equals(cls)) {
				flag = true;
				break;
			}
		}
		return flag;
	}

	/**
	 * 结束所有Activity
	 */
	public void finishAllActivity() {
		if (activityStack == null) {
			return;
		}
		for (int i = 0, size = activityStack.size(); i < size; i++) {
			if (null != activityStack.get(i)) {
				activityStack.get(i).finish();
			}
		}
		activityStack.clear();
	}

	/**
	 * 结束除了cls外的所有Activity,并导航到相应的cls
	 */
	public void intentToAtcivity(Activity activity, Class<?> cls) {
		if (activityStack == null) {
			return;
		}
		boolean flag = false;
		for (int i = 0, size = activityStack.size(); i < size; i++) {
			if (null != activityStack.get(i)) {
				Activity a = activityStack.get(i);
				if (!a.getClass().equals(cls)) {
					if (!a.equals(activity)) {
						activityStack.get(i).finish();
					}
				} else {
					flag = true;
					Intent intent = new Intent(activity, cls);
					intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
					activity.startActivity(intent);
				}
			}
		}
		if (!flag) {
			Intent intent = new Intent(activity, cls);
			activity.startActivity(intent);
		}
		activity.finish();
	}

	/**
	 * 
	 * @Title: finishActivitys
	 * @author 谢会超
	 * @Description: TODO(结束指定的几个activity)
	 * @param class_array
	 *            参数
	 * @date 创建(修改时间) 2015-10-30 上午10:11:17
	 * @return void 返回类型
	 */
	public void finishActivitys(HashSet<Class<?>> class_array) {
		if (activityStack == null) {
			return;
		}
		for (int i = 0, size = activityStack.size(); i < size; i++) {
			if (null != activityStack.get(i)) {
				Class<?> cls = activityStack.get(i).getClass();
				if (class_array.contains(cls)) {
					activityStack.get(i).finish();
				}
			}
		}
	}

	/**
	 * 
	 * @return true时表示activity数量为空
	 */
	public static boolean ActivityNumIsEmpty() {
		return activityStack.isEmpty();
	}

	/**
	 * 退出应用程序
	 */
	public void AppExit(Context context) {
		try {
			finishAllActivity();
			ActivityManager activityMgr = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
			activityMgr.restartPackage(context.getPackageName());
			System.exit(0);
		} catch (Exception e) {
		}
	}

	public static boolean isIndexPageReady() {
		List<File> files = FileUtil.searchFile(AppConfig.getWebRootDir(QApplication.context), PackegeConfig.INDEX_PAGE);
		return FileUtil.checkAssetsFileExit("www/" + PackegeConfig.INDEX_PAGE) || files.size() > 0;
	}
}
